package com.dushibao;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

/**
 * Velocity生成静态页面
 * @author wanghjbuf
 */
public class GenerHtmlForVelocity {
    private static final String DEF_ENCODING = "utf-8";

    public static void main(String []args){
        GenerHtmlForVelocity generHtmlForVelocity = new GenerHtmlForVelocity();
        generHtmlForVelocity.doRelease();
    }

    /**
     * 生成页面主体
     * @author wanghjbuf
     */
    public void doRelease(){
        try {
            Properties p = initVelocityProperties();
            Velocity.init(p);

            Template template = getVelocityTemplate();
            VelocityContext context = initVelocityContext();
            BufferedWriter writer = getWriterStream();

            template.merge(context, writer);
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 初始化VelocityProperties
     * @author wanghjbuf
     */
    public Properties initVelocityProperties() throws Exception{
        Properties p = new Properties();
        try{
            p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "");
            p.setProperty(Velocity.ENCODING_DEFAULT, DEF_ENCODING);
            p.setProperty(Velocity.INPUT_ENCODING, DEF_ENCODING);
            p.setProperty(Velocity.OUTPUT_ENCODING, DEF_ENCODING);
        }catch(Exception e){
            e.printStackTrace();
        }
        return p;
    }

    /**
     * 初始化VelocityContext
     * @author wanghjbuf
     */
    public VelocityContext initVelocityContext() throws Exception{
        VelocityContext context = new VelocityContext();
        try{
            List<String> blog_list = new ArrayList<String>();
            blog_list.add("wanghjtheory");
            blog_list.add("计算机组成原理");
            blog_list.add("JDK");
            blog_list.add("Oracle");
            blog_list.add("Tomcat");
            blog_list.add("Log4j");
            blog_list.add("FastJson");
            blog_list.add("Json");
            blog_list.add("反射");
            blog_list.add("SSI");
            blog_list.add("Velocity");
            blog_list.add("论文");
            blog_list.add("os X");
            blog_list.add("Js");
            blog_list.add("FreeMarker");
            blog_list.add("JSP");

            context.put("name", "wanghjbuf");
            context.put("blog_addr", "http://blog.csdn.net/wanghjbuf");
            context.put("blog_list", blog_list);
            context.put("last_modif", new Timestamp(System.currentTimeMillis()));
        }catch(Exception e){
            e.printStackTrace();
        }
        return context;
    }



    /**
     * 获取Velocity模板
     * @author wanghjbuf
     */
    public Template getVelocityTemplate() throws Exception{
        Template template = new Template();
        try{
            template = Velocity.getTemplate(GenerHtmlForVelocity.class.getClassLoader().getResource("myblog.html").getFile());
        }catch(Exception e){
            e.printStackTrace();
        }
        return template;
    }

    /**
     * 获取Velocity写入流
     * @author wanghjbuf
     */
    public BufferedWriter getWriterStream() throws Exception{
        try{
            File file = new File("src/main/resources/html");
            file.mkdirs();

            FileOutputStream fos = new FileOutputStream("src/main/resources/html/myblog.html");
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos, DEF_ENCODING));
            return writer;
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

}
